package com.bjgzy.courselive.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.di.component.DaggerCourseListComponent;
import com.bjgzy.courselive.di.module.CourseListModule;
import com.bjgzy.courselive.mvp.contract.CourseListContract;
import com.bjgzy.courselive.mvp.presenter.CourseListPresenter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CourseListData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-31 16:46:31
 */
@Route(path = RouterHub.COURSELIVE_COURSELISTFRAGMENT)
public class CourseListFragment extends BaseFragment<CourseListPresenter> implements CourseListContract.View {

    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    BaseQuickAdapter<CourseListData, AutoBaseViewHolder> mAdapter;
    private int internalId;

    public static CourseListFragment newInstance() {
        CourseListFragment fragment = new CourseListFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerCourseListComponent
                .builder()
                .appComponent(appComponent)
                .courseListModule(new CourseListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.courselive_fragment_list, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        swipeLayout.setOnRefreshListener(() -> getData(true));
//        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
//            if (view.getId() == R.id.btn_buy) {
//                Utils.navigationWithTitle(RouterHub.COURSELIVE_ORDERPAYACTIVITY, Constants.ORDERPAY);
//            } else if (view.getId() == R.id.btn_play) {
//                Utils.navigationWithTitle(RouterHub.COURSELIVE_COURSEPLAYACTIVITY, Constants.COURSE_PLAY);
//            }
//
//        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Utils.customNavigation(RouterHub.COURSELIVE_COURSEPLAYACTIVITY, Constants.COURSE_PLAY,mAdapter.getData().get(position));
        });
        mAdapter.setOnLoadMoreListener(() -> getData(false), rvList);
        rvList.setAdapter(mAdapter);
        rvList.setLayoutManager(layoutManager);
        getData(true);
    }

    private void getData(boolean pullToRefresh) {
        if (internalId>=0) {
            mPresenter.getCatalogById(pullToRefresh, internalId);
        }
    }

    @Override
    public void setData(@Nullable Object data) {
        this.internalId = (int) data;
    }

    @Override
    public void showLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void addFooter() {
//        View view = new View(getActivity());
//        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, RxUtil.getAutoHeight(getActivity(), 270));
//        view.setLayoutParams(layoutParams);
//        mAdapter.addFooterView(view);
    }

    @Override
    public int getParentId() {
        return internalId;
    }
}
